package com.doit.day04

import com.doit.day03.ScalaOrder

object _03_排序练习 {
  def main(args: Array[String]): Unit = {
    val orders: Array[ScalaOrder] = Array(
      ScalaOrder("001", "001", "001", "004", 25, 36),
      ScalaOrder("001", "002", "002", "007", 324, 44),
      ScalaOrder("002", "003", "003", "005", 84, 3),
      ScalaOrder("002", "004", "004", "004", 345, 53),
      ScalaOrder("002", "005", "005", "005", 869, 547),
      ScalaOrder("006", "006", "006", "005", 234, 43),
      ScalaOrder("007", "007", "007", "007", 4441, 168),
      ScalaOrder("008", "008", "008", "006", 656, 43),
      ScalaOrder("008", "009", "009", "006", 112, 75),
      ScalaOrder("010", "010", "010", "005", 312, 43)
    )

//    orders.sortBy(order=> -order.price*order.goods_num).foreach(println)

    //按照品类分组   key：品类  Array(所有相同平类得order)==》订单总额
/*    orders.groupBy(_.category_id)
      .map(tp=>{
        //tp._1  品类    tp._2=》Array(order) ==>map 将order 转变成 订单得销售额 Array(一个订单得销售额).sum=》所有订单得销售额
        (tp._1,tp._2.map(order=>order.goods_num*order.price).sum)
      }).foreach(println)

    orders.groupBy(_.user_id)
      .map(tp=>{
        //tp._1  用户    tp._2=》Array(order) ==>map 将order 转变成 订单得销售额 Array(一个订单得销售额).sum=》所有订单得销售额
        (tp._1,tp._2.map(order=>order.goods_num*order.price).sum)
      }).foreach(println)*/

    //map key user_id value Array(order)==>很多得订单里面求最大得一个订单金额得订单
    /*orders.groupBy(_.user_id)
      .map(tp => {
        //        tp._2.map(order=>order.goods_num*order.price).sortBy(price=> -price).take(1)
        //tp._2 ==> Array(user_id相同得所有order)
        //你想要得到什么？Array(user_id相同得所有order)中最大得那个order
        //如何去操作 返回一个order    take(1)==>第一个订单   ==》 只需要让他按照订单得金额进行降序排列
        //==>Array(order,order....)
        val value: Array[ScalaOrder] = tp._2
        val orders1: Array[ScalaOrder] = value.sortBy(order => -order.price * order.goods_num)
        val orders2 = orders1.take(1)(0)
        (tp._1, orders2)
        //将Array[ScalaOrder]里面唯一得一个order 取出来
        //就想用算子  ==》 想要干什么事情Array[ScalaOrder]   唯一得一个元素拿出来
        //        orders2.  所有得算子 ==》 list调用算子  返回就是list   用array调用算子，返回就是array
      }).foreach(println)*/


    orders
      .sortBy(order=> -order.price*order.goods_num)
      .take(2)
      .toList
      .foreach(println)

    orders
      .sortBy(order=> order.price*order.goods_num)
      .take(2)
      .toList
      .foreach(println)











  }

}
